/*
ForwardController
Version 1.0
Description: Controller class for Forward page.
Author: Pratibh Prakash
Created Date: 10 Feb-2009 
*/
public class ForwardController {

    private HR__c objHR, objTemp;
    private Map<String, String> parameters = new Map<String, String>();// Map for storing parameters and respective values from current PageReference. 
    private User user;
    
    
    /*
    Forward(): Constructor
    Initializes objHR with HR__c record in current context.
    */
    public ForwardController(ApexPages.StandardController controller) {
        
        objTemp = (HR__c)controller.getRecord();
       
        if(objTemp.Id!=null){//Validating if current page is HR__c detail view page.
            try{
                objHR = [select Id, First_Name__c, Last_Name__c , Email__c, User__c from HR__c where id=:objTemp.Id];
            }catch(Exception e){
                ApexPages.addMessages(e);
            }
            
        }
        else{
            try{
                PageReference currentPage = ApexPages.currentPage();
                parameters = currentPage.getParameters();
                user = [select id, Alias from User where id=:parameters.get('newid')];
                objHR = [select Id,Last_Name__c,User__c from HR__c where id=: parameters.get('hr')];
            }catch(Exception e){
                ApexPages.addMessages(e);
            }
        }
    }
    /*
    GotoUser()
    Description: Method used to redirect from HR__c record detail view to new User page.
    Method gets invoked on page load of ForwardUser VisualForce page load.  
    */
    
    public PageReference pageForward(){
        
        if(objTemp.Id!=null){//Validating if current page is HR__c detail view page.
            PageReference userPage;
            userPage = new PageReference('/005/e?retURL=%2F005%3FretURL%3D%252Fui%252Fsetup%252FSetup%253Fsetupid%253DUsers%26setupid%3DManageUsers' 
                                         + '&cancelURL=%2F'+ objHR.Id +'&saveURL=' + '/apex/Forward?hr='
                                         +objHR.Id);//Contructing PageReference with cancelURL and saveURL, passing Id of HR__c record as parameter in URL.
        
            userPage.getParameters().put('name_firstName',objHR.First_Name__c); 
            userPage.getParameters().put('name_lastName',objHR.Last_Name__c);
            userPage.getParameters().put('Email',objHR.Email__c);
            userPage.setRedirect(true);
            
            return userPage;
        }
        else{
            
            PageReference HRPage;
           
            objHR.User__c = user.id;
            HRPage = new PageReference('/'+objHR.Id);//Creating PageReference for HR__C detail view page.
            HRPage.setRedirect(true);
            update(objHR);
            return HRPage;
                       
        }
    }
    
    
        
    
}